//
//  CCMyCode.swift
//  swift_weibo
//
//  Created by ccl on 16/3/17.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit

class CCMyCode: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor();
        
        view.addSubview(showCodeImageV);
        showCodeImageV.frame.size = CGSize(width: 200, height: 200);
        //showCodeImageV.frame.height = 100;
        showCodeImageV.center = view.center;
        //showCodeImageV.backgroundColor = UIColor.yellowColor();
        
        showCodeImageV.image = creatMycode("我是陈春林");
    }
    
    //创建我的二维码
    private func creatMycode(message: String)->UIImage{
    // 建立二维码滤镜
     let ciFilter = CIFilter(name: "CIQRCodeGenerator")!;
     //设置初始值
        
        ciFilter.setDefaults()
        
        // 通过KVC设置滤镜二维码信息
        ciFilter.setValue(message.dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage");
        // 获取结果
        let ciImage = ciFilter.outputImage!;
        // 放大图片
        let transFrom = CGAffineTransformMakeScale(15, 15);
        
       let transImage = ciImage.imageByApplyingTransform(transFrom);
        // 颜色滤镜
        let colorImage =  creatColorCIImage(UIColor.orangeColor(), backgroundColor: UIColor.whiteColor(), inputImage: transImage);
        
        
        // 添加icon图片
        return insertIconImage(UIImage(CIImage: colorImage), iconImage:UIImage(named: "icon")! );
    }
    ///  给二维码添加中间图标,  中间的iconImage并不是镶嵌到二维码image中去而是盖在二维码上面, 二维码有一定的容错率, 所以可以正常读取数据. 但是二维码的四个角有三个角有正方形图快, 那个不能被遮挡, 那是二维码的定点.
    ///
    ///  - parameter codeImage: 二维码的image
    ///  - parameter iconImage: 要插入的image
    private func insertIconImage(codeImage:UIImage, iconImage: UIImage) -> UIImage{
    // 开启绘图
        
        UIGraphicsBeginImageContext(codeImage.size);
        // 绘图区域
        let drawRect = CGRect(origin: CGPointZero, size: codeImage.size);
        codeImage.drawInRect(drawRect);
        // 绘图
        let w = drawRect.width * 0.25;
        let x = (drawRect.width - w) * 0.5;
        let y = (drawRect.height - w) * 0.5;
        iconImage.drawInRect(CGRect(x: x, y: y, width: w, height: w));
        // 取出结果
        
        let resultImage = UIGraphicsGetImageFromCurrentImageContext();
        // 关闭绘图
        UIGraphicsEndImageContext();
        
        // 返回结果
        return resultImage;
    
    }
    
    
    // 系统提供的所用的滤镜
   // CIFilter.filterNamesInCategory(kCICategoryBuiltIn);
    // 添加颜色滤镜
    private func creatColorCIImage(tintColor:UIColor, backgroundColor:UIColor, inputImage:CIImage) -> CIImage{
     // 建立滤镜
        let colorFilter = CIFilter(name: "CIFalseColor")!;
        // 初始化
        colorFilter.setDefaults();
        // KVC设置内容
            //
        colorFilter.setValue(inputImage, forKey: "inputImage");
           // 对前景色修改
        colorFilter.setValue(CIColor(color: tintColor), forKey: "inputColor0");
          // 对背景色修改
        colorFilter.setValue(CIColor(color: backgroundColor), forKey: "inputColor1");
        
        // outputImage 获取CIImage
       return colorFilter.outputImage!;
    }
    
    // MARK: 懒加载
    lazy var showCodeImageV:UIImageView = UIImageView();
}
